//
//  AuthModel.swift
//  
//
//  Created by tank on 2021/11/16.
//

import Foundation

public class AuthModel: NSObject, Codable, NSSecureCoding {
    public static var supportsSecureCoding: Bool {
        return true
    }
    
    enum CodingKeys: CodingKey {
        case userId
        case uid
        case accessToken
        case accessTokenType
        case refreshToken
        case expiredTime
    }
    
    public let userId: String

    public var accessToken: String
    
    public let accessTokenType: String

    public var refreshToken: String

    public var expiredTime: Date
    
    
    public init(userId: String, accessToken: String, accessTokenType: String,
                refreshToken: String, expiredTime: Date) {
        self.userId = userId
        self.accessToken = accessToken
        self.accessTokenType = accessTokenType
        self.refreshToken = refreshToken
        self.expiredTime = expiredTime
    }
    
    required public init(from decoder: Decoder) throws {
        let container = try decoder.container(keyedBy: CodingKeys.self)
        
        self.userId = try (container.decodeIfPresent(String.self, forKey: .userId) ??
                           container.decode(String.self, forKey: .uid))
        
        self.accessToken = try container.decode(String.self, forKey: .accessToken)
        
        self.accessTokenType = try (container.decodeIfPresent(String.self, forKey: .accessTokenType) ?? "Bearer")
        
        self.refreshToken = try container.decode(String.self, forKey: .refreshToken)
        
        self.expiredTime = try (container.decodeIfPresent(Date.self, forKey: .expiredTime) ??
                                Calendar.current.date(byAdding: .day, value: 1, to: Date())!)
    }
    
    public func encode(to encoder: Encoder) throws {
        var container = encoder.container(keyedBy: CodingKeys.self)
        try container.encode(self.userId, forKey: .userId)
        try container.encode(self.accessToken, forKey: .accessToken)
        try container.encode(self.accessTokenType, forKey: .accessTokenType)
        try container.encode(self.refreshToken, forKey: .refreshToken)
        try container.encode(self.expiredTime, forKey: .expiredTime)
    }

    public func encode(with coder: NSCoder) {
        coder.encode(userId, forKey: "userId")
        coder.encode(accessToken, forKey: "accessToken")
        coder.encode(accessTokenType, forKey: "accessTokenType")
        coder.encode(refreshToken, forKey: "refreshToken")
        coder.encode(expiredTime, forKey: "expiredTime")
    }

    public required init?(coder: NSCoder) {
        userId = coder.decodeObject(forKey: "userId") as? String ?? ""
        accessToken = coder.decodeObject(forKey: "accessToken") as? String ?? ""
        accessTokenType = coder
            .decodeObject(forKey: "accessTokenType") as? String ?? ""
        refreshToken = coder.decodeObject(forKey: "refreshToken") as? String ?? ""
        expiredTime = coder.decodeObject(forKey: "expiredTime") as? Date ?? Date()
    }
}
